package com.wangshouyu.plugins.log.aop;

import com.wangshouyu.plugins.log.annotation.AopSlowMethodLog;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractPointcutAdvisor;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.util.Assert;


/**
 * @author 王守钰
 * @description aop慢方法日志
 * @date 2020/7/23 9:47
 */
public class AopSlowMethodLogMethodAnnotationAdvisor extends AbstractPointcutAdvisor implements BeanFactoryAware {

    private Advice advice;

    private Pointcut pointcut;

    public AopSlowMethodLogMethodAnnotationAdvisor(AopSlowMethodLogInterceptor advice) {
        Assert.notNull(advice,"AopSlowMethodLogInterceptor can not be null");
        this.advice = advice;
        this.pointcut = buildPointcut();
    }

    @Override
    public Pointcut getPointcut() {
        return this.pointcut;
    }

    @Override
    public Advice getAdvice() {
        return this.advice;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        if (this.advice instanceof BeanFactoryAware) {
            ((BeanFactoryAware) this.advice).setBeanFactory(beanFactory);
        }
    }

    private Pointcut buildPointcut() {
        return new AnnotationMatchingPointcut(null, AopSlowMethodLog.class);
    }
}